﻿#region 文件信息

/*----------------------------------------------------------------
//
// 文件名称：
// 文件功能描述：
// 设计要求：
//
// 文 件 名：    GeneralTreeContainer
// 创建者：      杨程
// 创建日期：	    2022/11/21 15:59:41

//----------------------------------------------------------------*/

#endregion

namespace Vampirewal.Core.GeneralTree;

/// <summary>
/// 通用树形容器
/// </summary>
public partial class GeneralTreeContainer<TTreeData> : ObservableObject, ITreePath
    where TTreeData : ITreeNode
{
    /// <summary>
    ///
    /// </summary>
    public GeneralTreeContainer(TTreeData treeNode)
    {
        //构造函数
        TreeNode = treeNode;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="parentNode"></param>
    /// <param name="treeNode"></param>
    public GeneralTreeContainer(GeneralTreeContainer<TTreeData> parentNode, TTreeData treeNode)
        : this(treeNode)
    {
        ParentNode = parentNode;
    }



    #region 属性

    /// <summary>
    /// 当前节点值
    /// </summary>
    public TTreeData TreeNode { get; private set; }

    /// <summary>
    /// 父节点，null表示顶节点
    /// </summary>
    public GeneralTreeContainer<TTreeData> ParentNode { get; set; }

    #region 是否选中

    /// <summary>
    /// 是否选中
    /// </summary>
    [ObservableProperty]
    private bool? isChecked = false;
    /// <summary>
    /// 是否选择
    /// </summary>
    [ObservableProperty]
    private bool isSelected = false;
    /// <summary>
    /// 是否展开
    /// </summary>
    [ObservableProperty]
    private bool isExpanded;

    #endregion

    #region 树内部层级
    /// <summary>
    /// 刷新层级
    /// </summary>
    /// <param name="rootIndex"></param>
    public void RefreshLevel(int rootIndex = -1)
    {
        if (ParentNode != null)
        {
            var levelIndex = ParentNode.SubItems.IndexOf(this) + 1;
            _treeLevelPath = $"{ParentNode.TreeLevelPath}-{levelIndex}";
        }
        else if (rootIndex >= 0)
        {
            _treeLevelPath = $"{rootIndex}";
        }
    }

    private string _treeLevelPath;

    /// <summary>
    /// 树内部层级
    /// </summary>
    public string TreeLevelPath
    {
        get
        {
            RefreshLevel();
            return _treeLevelPath;
        }
        internal set
        {
            _treeLevelPath = value;
            OnPropertyChanged();
        }
    }

    #endregion

    #region Methodes

    /// <summary>
    /// 获取子节点列表
    /// </summary>
    /// <param name="treeList">子节点列表集合</param>
    public void GetSubNodeList(ref List<GeneralTreeContainer<TTreeData>> treeList)
    {
        if (SubItems == null)
            return;
        foreach (var item in SubItems)
        {
            treeList.Add(item);
            item.GetSubNodeList(ref treeList);
        }
    }

    /// <summary>
    /// 根据<paramref name="delNodes"/>删除指定的节点数据
    /// </summary>
    /// <param name="isRemoveParentNode">当子节点清空后，是否删除父节点</param>
    /// <param name="delNodes">要删除的节点</param>
    public List<GeneralTreeContainer<TTreeData>> RemoveSubNode(bool isRemoveParentNode, ref List<TTreeData> delNodes)
    {
        List<GeneralTreeContainer<TTreeData>> delNodeList = new List<GeneralTreeContainer<TTreeData>>();
        if (delNodes.Count < 1 || SubItems == null)
            return delNodeList;

        for (int i = 0; i < SubItems.Count; i++)
        {
            var tmpDelNode = delNodes.FirstOrDefault(t => t.IsSameNode(SubItems[i].TreeNode));
            if (tmpDelNode != null)
            {
                //获取被删除节点下的所有子节点
                delNodeList.Add(SubItems[i]);
                SubItems[i].GetSubNodeList(ref delNodeList);
                //删除子节点
                SubItems.RemoveAt(i);
                delNodes.Remove(tmpDelNode);
                i--;
            }
            else if (SubItems[i].SubItems != null)
            {
                delNodeList.AddRange(SubItems[i].RemoveSubNode(isRemoveParentNode, ref delNodes));
                if (isRemoveParentNode && SubItems[i].SubItems.Count < 1)
                {
                    //子节点清空后，删除对应父节点，添加节点到已删除列表
                    delNodeList.Add(SubItems[i]);

                    SubItems.RemoveAt(i);
                    i--;
                }
            }
        }
        return delNodeList;
    }

    /// <summary>
    /// 添加子节点
    /// </summary>
    /// <param name="subItems">要添加的子节点</param>
    public void AppendSubNode(IEnumerable<GeneralTreeContainer<TTreeData>> subItems)
    {
        foreach (var item in subItems)
        {
            item.ParentNode = this;
        }
        if (SubItems == null)
            SubItems = new ObservableCollection<GeneralTreeContainer<TTreeData>>(subItems);
        else
        {
            foreach (var item in subItems)
            {
                SubItems.Add(item);
            }
        }
    }

    /// <summary>
    /// 获取树路径
    /// </summary>
    /// <returns></returns>
    public Queue<int> GetTreePath()
    {
        Queue<int> ints = new Queue<int>();

        var Levels = TreeLevelPath.Split('-').Select(s => Convert.ToInt32(s)).ToList();
        foreach (var item in Levels)
        {
            ints.Enqueue(item);
        }

        return ints;
    }

    /// <summary>
    /// 展开对应的层级节点
    /// </summary>
    /// <param name="levelNum">层级</param>
    /// <param name="isExpand">是否折叠，true：展开，false：折叠</param>
    public void ExpandNodeByLevelNumber(int levelNum, bool isExpand)
    {
        var curLevel = TreeLevelPath.Split('-').Length;
        if (SubItems != null && SubItems.Count > 0)
        {
            foreach (var item in SubItems)
            {
                item.ExpandNodeByLevelNumber(levelNum, isExpand);
            }

            if (curLevel <= levelNum)
            {
                //展开节点
                IsExpanded = isExpand;
            }
            else
                IsExpanded = false;
        }
    }

    #endregion

    #region 子节点

    /// <summary>
    /// 子节点
    /// </summary>
    [ObservableProperty]
    private ObservableCollection<GeneralTreeContainer<TTreeData>> subItems;

    #endregion

    #endregion
}